﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class StoredProceduresDocument : Document
    {
        #region internal
        //Constructor
        internal StoredProceduresDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.StoredProcedures;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "storedprocedures.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Stored Procedures"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"       .parameterColumn { width: 200px; word-wrap: break-word;}");
                AddCssLine(@"      	.createdOnColumn { width: 15%; }");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Stored Procedures</h2>");

                Boolean evenRow = true;
                var schemas = (from schema in _database.Schemas
                               orderby schema.Name
                               select schema);
                foreach (SqlServer.Schema schema in schemas)
                {
                    var sprocs = (from sproc in schema.StoredProcedures
                                  orderby sproc.Name
                                  select sproc);
                    if (sprocs.Count() != 0)
                    {
                        evenRow = true;

                        AddBodyLine(@"<h2>");
                        AddBodyLine(@"<img id=""img_" + schema.Name + @""" src=""" + RootFolderPath + @"minus.jpg""> ");
                        AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('" + schema.Name + @"');"">" + schema.Name + @"</a>");
                        AddBodyLine(@"</h2>");

                        AddBodyLine(@"<div id=""" + schema.Name + @""">");
                        AddBodyLine(@"<table>");
                        AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""parameterColumn"" /><col class=""createdOnColumn"" /><col class=""descriptionColumn"" /></colgroup>");
                        AddBodyLine(@"<tr><th>Name</th><th>Parameters</th><th>Created On</th><th>Description</th></tr>");

                        evenRow = true;
                        foreach (SqlServer.StoredProcedure storedProcedure in sprocs)
                        {
                            StringBuilder sb = new StringBuilder();

                            if (evenRow == true)
                                sb.Append(@"<tr class=""evenRow"">");
                            else
                                sb.Append(@"<tr class=""oddRow"">");
                            evenRow = !evenRow;

                            string href = @"<td>";
                            href += @"<a href=""http://index." + RootDatabase.Name + "." + 
                                storedProcedure.Schema.Name + @".storedprocedure." + storedProcedure.Name + @".html"">" +
                                ScrubHtml(storedProcedure.Name) + @"</a>";
                            href += @"</td>";
                            sb.Append(href);

                            if (storedProcedure.Parameters.Count() == 0)
                               href = @"<td>None</td>";
                            else
                            {
                               href = @"<td>";
                               foreach (SqlServer.Parameter parameter in storedProcedure.Parameters)
                               {
                                  href += parameter.Name + ", ";
                               }
                               href = href.TrimEnd(@" ,".ToCharArray());
                               href += @"</td>";
                            }
                            sb.Append(href);

                            sb.Append(@"<td>" + storedProcedure.CreateDate.ToString() + @"</td>");
                            if (storedProcedure.Description == "")
                                sb.Append(@"<td>&nbsp;</td></tr>");
                            else
                                sb.Append(@"<td>" + ScrubHtml(storedProcedure.Description) + @"</td></tr>");

                            AddBodyLine(sb.ToString());
                        }

                        AddBodyLine(@"</table>");
                        AddBodyLine(@"</div>");
                    }
                }
                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
